<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="姓名">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="status">
        <el-select v-model="queryParams.sex" clearable placeholder="请选择性别">
          <el-option
            v-for="dict in sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" style="font-weight: 600">
        <el-input v-model="queryParams.minAge" clearable style="width: 80px" />
        ~
        <el-input v-model="queryParams.maxAge" clearable style="width: 80px" />
      </el-form-item>
      <el-form-item label="身份证号码">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务部门" prop="status">
        <!--        <el-select v-model="queryParams.status" placeholder="请选择服务部门" clearable>-->
        <!--          <el-option-->
        <!--              v-for="dict in sys_normal_disable"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="dict.value"-->
        <!--          />-->
        <!--        </el-select>-->
        <el-tree-select
          clearable
          v-model="queryParams.deptId"
          :data="deptOptions"
          :props="{ value: 'id', label: 'label', children: 'children' }"
          check-strictly
          placeholder="请选择归属部门"
          value-key="id"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['oldManger:record:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oldManger:record:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="postCode">
        <template #default="scope">
          <span>
            {{
              sys_user_sex.find((item) => item.value == scope.row.sex)
                ? sys_user_sex.find((item) => item.value == scope.row.sex).label
                : ""
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="出生日期" align="center" prop="birthDate" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="联系电话" align="center" prop="contacterPhone" />
      <el-table-column label="服务部门" align="center" prop="deptName" />

      <el-table-column label="户籍地址" align="center" prop="postCode">
        <template #default="scope">
          <spna
            >{{ scope.row.regAddrProvinceName }} {{ scope.row.regAddrCityName
            }}{{ scope.row.regAddrCountyName }}
            {{ scope.row.regAddrStreetName }}
            {{ scope.row.regDetailAddress }}</spna
          >
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['oldManger:record:edit']"
            >修改</el-button
          >
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oldManger:record:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="650px"
      append-to-body
      v-if="open"
    >
      <el-form
        ref="postRef"
        :model="form"
        :rules="rules"
        label-width="120px"
        style="height: 700px; overflow: auto"
      >
        <el-scrollbar class="custom-scrollbar">
          <el-row>
            <el-col :span="24">
              <el-form-item label="服务部门" prop="deptId">
                <el-tree-select
                  style="width: 500px"
                  @change="change"
                  clearable
                  v-model="form.deptId"
                  :data="deptOptions"
                  :props="{ value: 'id', label: 'label', children: 'children' }"
                  check-strictly
                  placeholder="请选择归属部门"
                  value-key="id"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="助老员">
                <el-select
                  v-model="form.elderHelperList"
                  multiple
                  clearable
                  placeholder="请选择助老员"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in elderHelpers"
                    :key="dict.userId"
                    :label="dict.userName"
                    :value="dict.userId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" placeholder="请输入姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="曾用名" prop="usedName">
                <el-input v-model="form.usedName" placeholder="请输入曾用名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="sex">
                <el-select
                  v-model="form.sex"
                  clearable
                  placeholder="请选择性别"
                >
                  <el-option
                    v-for="dict in sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="民族" prop="nation">
                <el-input v-model="form.nation" placeholder="请输入民族" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证号码" prop="idCard">
                <el-input
                  v-model="form.idCard"
                  placeholder="请输入身份证号码"
                  @blur="blurIdCard"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="telephone">
                <el-input
                  v-model="form.telephone"
                  placeholder="请输入联系电话"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出生年月">
                <el-input
                  v-model="form.birthDate"
                  :disabled="true"
                  placeholder="请输入出生年月"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年龄">
                <el-input
                  v-model="form.age"
                  :disabled="true"
                  placeholder="请输入年龄"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="文化程度" prop="educationLevel">
                <el-select
                  v-model="form.educationLevel"
                  clearable
                  placeholder="请选择文化程度"
                >
                  <el-option
                    v-for="dict in education_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="经济状况" prop="economicSituation">
                <el-select
                  v-model="form.economicSituation"
                  clearable
                  placeholder="请选择经济状况"
                >
                  <el-option
                    v-for="dict in economic_situation"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="居住状况" prop="livingConditions">
                <el-select
                  v-model="form.livingConditions"
                  clearable
                  placeholder="请选择居住状况"
                >
                  <el-option
                    v-for="dict in living_conditions"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="政治面貌">
                <el-input
                  v-model="form.politicalOutlook"
                  placeholder="请输入政治面貌"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="紧急联系人" prop="emergencyContacter">
                <el-input
                  v-model="form.emergencyContacter"
                  placeholder="请输入紧急联系人"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人电话" prop="contacterPhone">
                <el-input
                  v-model="form.contacterPhone"
                  placeholder="请输入联系人电话"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="宗教信仰">
                <el-input
                  v-model="form.religiousBelief"
                  placeholder="请输入宗教信仰"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="兴趣爱好">
                <el-input v-model="form.hobby" placeholder="请输入兴趣爱好" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="原工作单位">
                <el-input
                  v-model="form.originalUnit"
                  placeholder="请输入原工作单位"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="原职业">
                <el-input
                  v-model="form.originalOccupation"
                  placeholder="请输入原职业"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label="户籍所在地"
                prop="regAddrStreetId"
                style="font-weight: 700"
              >
                <el-select
                  v-model="form.regAddrProvinceId"
                  placeholder="选择省"
                  style="width: 100px"
                  @change="changeProvince"
                >
                  <el-option
                    v-for="dict in regionList"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>
                <el-select
                  v-model="form.regAddrCityId"
                  placeholder="选择市"
                  style="width: 100px; margin-left: 10px"
                  @change="changeCity"
                >
                  <el-option
                    v-for="dict in CityList"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>
                <el-select
                  v-model="form.regAddrCountyId"
                  placeholder="选择区/县"
                  style="width: 100px; margin-left: 10px"
                  @change="changeCounty"
                >
                  <el-option
                    v-for="dict in countyList"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>

                <el-select
                  v-model="form.regAddrStreetId"
                  placeholder="选择乡村街道"
                  style="width: 100px; margin-left: 10px"
                  @change="changeStreet"
                >
                  <el-option
                    v-for="dict in streetList"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="户籍详细地址" prop="regDetailAddress">
                <el-input
                  v-model="form.regDetailAddress"
                  placeholder="请输入户籍详细地址"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label="常住地址"
                prop="livingAddrProvinceId"
                style="font-weight: 700"
              >
                <el-select
                  v-model="form.livingAddrProvinceId"
                  placeholder="选择省"
                  style="width: 100px"
                  @change="changelivingProvince"
                >
                  <el-option
                    v-for="dict in livingRegionList"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>
                <el-select
                  v-model="form.livingAddrCityId"
                  placeholder="选择市"
                  style="width: 100px; margin-left: 10px"
                  @change="changelivingCity"
                >
                  <el-option
                    v-for="dict in livingCityList"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>
                <el-select
                  v-model="form.livingAddrCountyId"
                  placeholder="选择区/县"
                  style="width: 100px; margin-left: 10px"
                  @change="changelivingCounty"
                >
                  <el-option
                    v-for="dict in livingCountyList"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>

                <el-select
                  v-model="form.livingAddrStreetId"
                  placeholder="选择乡村街道"
                  style="width: 100px; margin-left: 10px"
                  @change="changelivingStreet"
                >
                  <el-option
                    v-for="dict in livingStreetList"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>

                <el-select
                  v-model="form.livingAddrCommunityId"
                  placeholder="选择乡村社区"
                  @change="changelivingCommunity"
                  style="width: 200px; margin-top: 15px"
                >
                  <el-option
                    v-for="dict in livingAddrCommunity"
                    :key="dict.id"
                    :label="dict.name"
                    :value="dict.areaCode"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="常住详细地址" prop="livingDetailAddress">
                <el-input
                  v-model="form.livingDetailAddress"
                  placeholder="请输入常住详细地址"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="assessResult">
                <template #label>
                  <div
                    class="custom-label"
                    style="line-height: 18px; font-weight: 700"
                  >
                    失能等级(评估结果)
                  </div>
                </template>
                <el-select
                  v-model="form.assessResult"
                  clearable
                  placeholder="请选择失能等级(评估结果)"
                >
                  <el-option
                    v-for="dict in assess_result"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="roleName">
                <template #label>
                  <div
                    class="custom-label"
                    style="line-height: 18px; font-weight: 700"
                  >
                    失能等级(评估)报告(PDF)
                  </div>
                </template>
                <el-upload
                  :file-list="fileList"
                  class="upload-demo"
                  :action="url"
                  :headers="upload.headers"
                  :on-success="handleUploadSuccess"
                  :on-remove="handleRemove"
                  :before-remove="beforeRemove"
                >
                  <el-link type="primary">添加报告</el-link>
                </el-upload>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="主要慢性病情况">
                <el-checkbox-group v-model="form.chronicDiseaseType">
                  <el-checkbox
                    v-for="dict in chronic_diseases_type"
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-checkbox
                  >
                </el-checkbox-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label="其它慢性病情况"
                prop="otherChronicDiseaseType"
              >
                <el-input
                  v-model="form.otherChronicDiseaseType"
                  placeholder="请输入常其它慢性病情况"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="isCareInsurance">
                <template #label>
                  <div class="custom-label" style="line-height: 18px">
                    是否享受长期护理保险
                  </div>
                </template>
                <el-select
                  v-model="form.isCareInsurance"
                  placeholder="请选择"
                  style="width: 200px; margin-top: 15px"
                >
                  <el-option
                    v-for="dict in is_care_insurance"
                    :key="dict.id"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="isDisableSubsidy">
                <template #label>
                  <div class="custom-label" style="line-height: 18px">
                    是否享受重度残疾人护理补贴
                  </div>
                </template>
                <el-select
                  v-model="form.isDisableSubsidy"
                  placeholder="请选择"
                  style="width: 200px; margin-top: 15px"
                >
                  <el-option
                    v-for="dict in is_disable_subsidy"
                    :key="dict.id"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="isElderlySubsidy">
                <template #label>
                  <div class="custom-label" style="line-height: 18px">
                    是否享受养老服务补贴
                  </div>
                </template>
                <el-select
                  v-model="form.isElderlySubsidy"
                  placeholder="请选择"
                  style="width: 200px; margin-top: 15px"
                >
                  <el-option
                    v-for="dict in is_elderly_subsidy"
                    :key="dict.id"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" class="Medical">
              <el-form-item>
                <template #label>
                  <div class="custom-label" style="margin-top: 15px">
                    <span style="color: #f56c6c; margin-right: 5px">*</span
                    >是否享受医保
                  </div>
                </template>
                <el-select
                  v-model="form.isMedicalInsurance"
                  placeholder="请选择"
                  style="width: 200px; margin-top: 15px"
                >
                  <el-option
                    v-for="dict in is_medical_insurance"
                    :key="dict.id"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-scrollbar>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormDebounceHandler"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import { debounce } from "@/utils/debounce.js";
import {
  elderArchive,
  elderArchiveDel,
  systemUser,
  region,
  addelderArchive,
  elderArchiveinfo,
  updateelderArchive,
} from "@/api/record/api";
import { treeselect } from "@/api/system/dept";
import { getToken } from "@/utils/auth";
const { proxy } = getCurrentInstance();
const {
  sys_normal_disable,
  sys_user_sex,
  education_level,
  economic_situation,
  living_conditions,
  assess_result,
  chronic_diseases_type,
  is_care_insurance,
  is_disable_subsidy,
  is_elderly_subsidy,
  is_medical_insurance,
} = proxy.useDict(
  "sys_normal_disable",
  "sys_user_sex",
  "education_level",
  "economic_situation",
  "living_conditions",
  "assess_result",
  "chronic_diseases_type",
  "is_care_insurance",
  "is_disable_subsidy",
  "is_elderly_subsidy",
  "is_medical_insurance"
);
const deptOptions = ref(undefined);
const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const elderHelpers = ref([]);
const regionList = ref([]);
const CityList = ref([]);
const countyList = ref([]);
const streetList = ref([]);

const livingRegionList = ref([]);
const livingCityList = ref([]);
const livingCountyList = ref([]);
const livingStreetList = ref([]);
const livingAddrCommunity = ref([]);
const url = ref(process.env.VUE_APP_BASE_API + "/file/upload");
/*** 用户导入参数 */
const upload = reactive({
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
});
const validateIdCard = (rule, value, callback) => {
  // 利用正则表达式验证身份证号码
  const pattern =
    /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[0-9Xx]$/;
  if (!value || pattern.test(value)) {
    callback(); // 验证通过
  } else {
    callback(new Error("身份证号码格式不正确")); // 验证不通过，返回错误信息
  }
};
const fileList = ref([]);

const data = reactive({
  form: {},

  queryParams: {
    page: 1,
    limit: 10,
    maxAge: undefined,
    minAge: undefined,
    name: undefined,
    sex: undefined,
    idCard: undefined,
    deptId: undefined,
  },
  rules: {
    deptId: [{ required: true, message: "请选择部门", trigger: "change" }],
    name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
    usedName: [{ required: true, message: "曾用名不能为空", trigger: "blur" }],
    sex: [{ required: true, message: "请选择性别", trigger: "change" }],
    nation: [{ required: true, message: "请输入民族", trigger: "blur" }],
    idCard: [
      { required: true, message: "请输入身份证号码", trigger: "blur" },
      { validator: validateIdCard, trigger: "blur" },
    ],
    telephone: [
      { required: true, message: "请输入正确手机号", trigger: "blur" },
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: "请输入正确的手机号码",
        trigger: "blur",
      },
    ],
    educationLevel: [
      { required: true, message: "请选择文化程度", trigger: "change" },
    ],
    economicSituation: [
      { required: true, message: "请选择经济状况", trigger: "change" },
    ],
    livingConditions: [
      { required: true, message: "请选择居住状况", trigger: "change" },
    ],
    emergencyContacter: [
      { required: true, message: "紧急联系人不能为空", trigger: "blur" },
    ],
    contacterPhone: [
      { required: true, message: "联系人电话不能为空", trigger: "blur" },
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: "请输入正确的手机号码",
        trigger: "blur",
      },
    ],
    regAddrProvinceId: [
      { required: true, message: "请选择户籍所在地", trigger: "change" },
    ],
    regDetailAddress: [
      { required: true, message: "户籍详细地址不能为空", trigger: "blur" },
    ],
    livingDetailAddress: [
      { required: true, message: "常住详细地址不能为空", trigger: "blur" },
    ],
    assessResult: [
      {
        required: true,
        message: "请选择失能等级(评估结果)",
        trigger: "change",
      },
    ],
    livingAddrProvinceId: [
      { required: true, message: "常住地址不能为空", trigger: "change" },
    ],
    isCareInsurance: [
      {
        required: true,
        message: "请选择是否长期享受护理保险",
        trigger: "change",
      },
    ],
    isDisableSubsidy: [
      {
        required: true,
        message: "请选择是否享受残疾人护理补贴",
        trigger: "change",
      },
    ],
    isElderlySubsidy: [
      { required: true, message: "请选择养老服务补贴", trigger: "change" },
    ],
    isMedicalInsurance: [
      { required: true, message: "请选择是否享受医保", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);
function handleUploadSuccess(res, file, fileList) {
  console.log("url", url.value);
  let arr = [];
  for (let i in fileList) {
    console.log(fileList[i]);
    if (fileList[i].response) {
      arr.push({
        name: fileList[i].response.data.name,
        url: fileList[i].response.data.url,
      });
    } else {
      arr.push({
        name: fileList[i].name,
        url: fileList[i].url,
      });
    }
  }
  console.log("22222222222", arr);
  form.value.assessReportUrlList = arr;
}

function handleRemove(files, fileList) {
  let arr = [];
  for (let i in fileList) {
    console.log(fileList[i]);
    if (fileList[i].response) {
      arr.push({
        name: fileList[i].response.data.name,
        url: fileList[i].response.data.url,
      });
    } else {
      arr.push({
        name: fileList[i].name,
        url: fileList[i].url,
      });
    }
  }
  form.value.assessReportUrlList = arr;
}
function beforeRemove(uploadFile, uploadFiles) {}

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  elderArchive(queryParams.value).then((response) => {
    console.log("response", response);
    postList.value = response.page.list;
    total.value = response.page.totalCount;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
function getTreeselect() {
  treeselect().then((response) => {
    deptOptions.value = response.data;
  });
}

/** 表单重置 */
function reset() {
  form.value = {
    deptId: undefined,
    elderHelperList: [],
    name: undefined,
  };
  proxy.resetForm("postRef");
}
function change(val) {
  console.log(val);
  elderHelpers.value = [];
  form.value.elderHelperList = [];
  systemUser(val).then((res) => {
    console.log(res);
    elderHelpers.value = res.rows;
  });
}
// 身份证验证
function blurIdCard(val) {
  form.value.birthDate = getBirthDate(form.value.idCard);
  form.value.age = calculateAgeFromIdNumber(form.value.idCard);
}
function getBirthDate(idNumber) {
  // 身份证号码的前14位为出生年月日信息
  var birthString = idNumber.substring(6, 14);

  // 提取年、月、日
  var year = birthString.substring(0, 4);
  var month = birthString.substring(4, 6);
  var day = birthString.substring(6, 8);

  // 返回格式化后的出生年月日
  return year + "-" + month + "-" + day;
}
function calculateAgeFromIdNumber(idNumber) {
  // 获取当前日期
  var currentDate = new Date();

  // 提取出生年月日信息
  var birthYear = parseInt(idNumber.substring(6, 10));
  var birthMonth = parseInt(idNumber.substring(10, 12));
  var birthDay = parseInt(idNumber.substring(12, 14));

  // 提取当前日期的年、月、日
  var currentYear = currentDate.getFullYear();
  var currentMonth = currentDate.getMonth() + 1;
  var currentDay = currentDate.getDate();

  // 计算年龄
  var age = currentYear - birthYear;

  // 判断是否已过生日
  if (
    currentMonth < birthMonth ||
    (currentMonth === birthMonth && currentDay < birthDay)
  ) {
    age--;
  }

  return age;
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  (queryParams.value = {
    page: 1,
    limit: 10,
    maxAge: undefined,
    minAge: undefined,
    name: undefined,
    sex: undefined,
    idCard: undefined,
    deptId: undefined,
  }),
    handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  console.log(ids.value);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加长者";
}
/** 修改按钮操作 */

async function handleUpdate(row) {
  reset();
  const postId = row.id;
  const data = await elderArchiveinfo(postId);
  for (let i in data.elderArchiveDTO.elderHelperList) {
    data.elderArchiveDTO.elderHelperList[i] =
      data.elderArchiveDTO.elderHelperList[i] - 0;
  }
  const data8 = await systemUser(data.elderArchiveDTO.deptId);
  elderHelpers.value = data8.rows;
  title.value = "修改长者信息";
  open.value = true;
  data.elderArchiveDTO.chronicDiseaseType = data.elderArchiveDTO
    .chronicDiseaseType
    ? data.elderArchiveDTO.chronicDiseaseType
    : [];

  form.value = data.elderArchiveDTO;
  fileList.value = data.elderArchiveDTO.assessReportUrlList;

  const data1 = await region(data.elderArchiveDTO.regAddrProvinceId);
  const data2 = await region(data.elderArchiveDTO.regAddrCityId);
  const data3 = await region(data.elderArchiveDTO.regAddrCountyId);
  const data4 = await region(data.elderArchiveDTO.livingAddrProvinceId);
  const data5 = await region(data.elderArchiveDTO.livingAddrCityId);
  const data6 = await region(data.elderArchiveDTO.livingAddrCountyId);
  const data7 = await region(data.elderArchiveDTO.livingAddrStreetId);

  CityList.value = data1.data;
  countyList.value = data2.data;
  streetList.value = data3.data;
  livingCityList.value = data4.data;
  livingCountyList.value = data5.data;
  livingStreetList.value = data6.data;
  livingAddrCommunity.value = data7.data;
}

/** 提交按钮 */
const submitFormDebounceHandler = debounce(submitForm, 500);
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateelderArchive(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addelderArchive(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  console.log(row, ids.value);
  const postIds = row.id ? [row.id] : ids.value;
  console.log([postIds]);
  proxy.$modal
    .confirm("是否确认删除该数据项？")
    .then(function () {
      return elderArchiveDel(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
function changeProvince(val) {
  form.value.regAddrProvinceName = regionList.value.find(
    (item) => item.areaCode == val
  )?.name;
  CityList.value = [];
  form.value.regAddrCityId = "";
  form.value.regAddrCountyId = "";
  form.value.regAddrStreetId = "";
  countyList.value = [];
  streetList.value = [];
  region(val).then((res) => {
    CityList.value = res.data;
  });
}
function changeCity(val) {
  form.value.regAddrCityName = CityList.value.find(
    (item) => item.areaCode == val
  )?.name;
  form.value.regAddrCountyId = "";
  form.value.regAddrStreetId = "";
  countyList.value = [];
  streetList.value = [];
  region(val).then((res) => {
    countyList.value = res.data;
  });
}
function changeCounty(val) {
  form.value.regAddrCountyName = countyList.value.find(
    (item) => item.areaCode == val
  )?.name;
  form.value.regAddrStreetId = "";
  streetList.value = [];
  region(val).then((res) => {
    console.log(res);
    streetList.value = res.data;
  });
}
function changeStreet(val) {
  form.value.regAddrStreetName = streetList.value.find(
    (item) => item.areaCode == val
  )?.name;
}

function changelivingProvince(val) {
  form.value.livingAddrProvinceName = livingRegionList.value.find(
    (item) => item.areaCode == val
  )?.name;
  livingCityList.value = [];
  form.value.livingAddrCityId = "";
  form.value.livingAddrCountyId = "";
  form.value.livingAddrStreetId = "";
  livingCountyList.value = [];
  livingStreetList.value = [];
  region(val).then((res) => {
    console.log(res);
    livingCityList.value = res.data;
  });
}
function changelivingCity(val) {
  form.value.livingAddrCityName = livingCityList.value.find(
    (item) => item.areaCode == val
  )?.name;
  form.value.livingAddrCountyId = "";
  form.value.livingAddrStreetId = "";
  livingCountyList.value = [];
  livingStreetList.value = [];
  region(val).then((res) => {
    console.log(res);
    livingCountyList.value = res.data;
  });
}
function changelivingCounty(val) {
  form.value.livingAddrCountyName = livingCountyList.value.find(
    (item) => item.areaCode == val
  )?.name;
  form.value.livingAddrStreetId = "";
  form.value.livingAddrCommunityId = "";
  livingStreetList.value = [];

  region(val).then((res) => {
    console.log(res);
    livingStreetList.value = res.data;
  });
}
function changelivingStreet(val) {
  console.log("val", val);
  form.value.livingAddrStreetName = livingStreetList.value.find(
    (item) => item.areaCode == val
  )?.name;
  livingAddrCommunity.value = [];
  form.value.livingAddrCommunityId = "";
  region(val).then((res) => {
    console.log(res);
    livingAddrCommunity.value = res.data;
  });
}
function changelivingCommunity(val) {
  form.value.livingAddrCommunityName = livingAddrCommunity.value.find(
    (item) => item.areaCode == val
  )?.name;
}

/** 导出按钮操作 */
function getregion() {
  region(0).then((res) => {
    console.log(res);
    regionList.value = res.data;
    livingRegionList.value = res.data;
  });
}

getList();
getTreeselect();
getregion();
</script>
<style></style>
